package com.zimmor.mq.rocket.order;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/11/18 15:22
 * @Description 消费者之负载均衡模式
 * @version: 1.0.0
 */

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者，设置消费者groupId
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.设置namesrv地址
        consumer.setNamesrvAddr("192.168.204.128:9876;192.168.204.129:9876");
        //3.订阅topic和tag
        consumer.subscribe("order", "tag1");
        //4.注册监听，处理消息
        consumer.registerMessageListener(new MessageListenerOrderly() {
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt msg : list) {
                    System.out.println(Thread.currentThread().getName() + "---" + new String(msg.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //5.启动consumer
        consumer.start();
    }
}
